package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.FlavorMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;

import org.apache.xmlbeans.impl.jam.mutable.MElement;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Slf4j
@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private FlavorMapper flavorMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 新增菜品
     *
     * @param dishDTO
     */

    @Transactional
    public void saveWithFlavor(DishDTO dishDTO) {
        //拷贝数据
        Dish dish=new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        //调用数据库,完成新增
        dishMapper.saveDish(dish);
        //获取dish_id
        Long id=dish.getId();
        log.info("获取菜品id:{}", id);
        //获取口味集合
        List<DishFlavor> flavors=dishDTO.getFlavors();
        if (flavors != null && !flavors.isEmpty()) {
            //循环遍历集合,插入主键id
            flavors.forEach(dishFlavor -> dishFlavor.setDishId(id));
            flavorMapper.saveFlavor(flavors);
        }

    }

    /**
     * 菜品分页查询
     *
     * @return
     */
    @Override
    public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        //设置分页参数
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        //调用数据库查询信息
        Page<DishVO> page=dishMapper.pageQuery(dishPageQueryDTO);

        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 删除菜品
     *
     * @param ids
     */
    @Transactional
    public void deleteDish(List<Long> ids) {
        //可以一次删除一个菜品,也可以批量删除菜品
        //循环遍历集合
        ids.forEach(id -> {
            Integer status=dishMapper.selectDishStatus(id);
            //起售中的菜品不能删除
            if (status == 1) {
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
            //被套餐关联的菜品不能删除
            Integer count=setmealMapper.selectSetmealWithDish(id);
            if (count > 0) {
                throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
            }
            dishMapper.deleteDish(id);
            //删除菜品后,关联的口味数据也要删除掉
            flavorMapper.deleteFlavor(id);
        });


    }

    /**
     * 根据菜品id回显
     *
     * @param id
     * @return
     */
    @Override
    public DishVO getById(Long id) {
        DishVO dishVO=dishMapper.getById(id);
        //回显口味数据
        List<DishFlavor> flavorList=flavorMapper.getById(id);
        //将口味数据插入到dishVo中
        dishVO.setFlavors(flavorList);
        return dishVO;
    }

    /**
     * 修改菜品
     *
     * @param dishDTO
     */
    @Transactional
    public void updateDish(DishDTO dishDTO) {
        //拷贝数据
        Dish dish=new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        //调用数据库,完成修改
        dishMapper.updateDish(dish);
        //获取菜品id
        Long id=dish.getId();
        //删除原有口味
        flavorMapper.deleteFlavor(id);
        //新增口味并且保存
        //获取口味集合
        List<DishFlavor> flavors=dishDTO.getFlavors();
        if (flavors != null && !flavors.isEmpty()) {
            //循环遍历集合,插入主键id
            flavors.forEach(dishFlavor -> dishFlavor.setDishId(id));
            flavorMapper.saveFlavor(flavors);
        }
    }

    /**
     * 菜品起售停售
     *
     * @param id,status
     */
    @Override
    public void updateDishStatus(String status, Long id) {
        //直接操作数据库,完成修改
        dishMapper.updateDishStatus(status, id);
    }

    /**
     * 根据分类id查询菜品
     *
     * @param categoryId
     * @return
     */
    @Override
    public List<DishVO> list(Long categoryId) {
        //操作数据库,完成查询
        List<DishVO> list=dishMapper.list(categoryId);
        return list;
    }
}
